package com.example.jsoup.kuang.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by HMF on 2021/10/16 16:10
 */

/***
 * 表单提交类型是 request_payload 的基础爬取方法
 *
 * 你需要通过这个 demo 提取
 */
public class RequestPayloadDemo {

    public static void main(String[] args) throws Exception {

        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建httpGet对象，设置url地址：科创中国网
        HttpPost httpPost = new HttpPost("https://www.qiyekexie.com/qykxexpert/expert/getPublicExpert");

//        httpPost.addHeader("accept", "application/json, text/javascript, */*; q=0.01");
//        httpPost.addHeader("accept-encoding", "gzip, deflate, br");
//        httpPost.addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8");
//        httpPost.addHeader("connection", "keep-alive"); ///
//        httpPost.addHeader("content-length", "147"); ///
//        httpPost.addHeader("content-type", "application/json");
//        httpPost.addHeader("cookie", "acw_tc=2f624a7416339131728998385e4a7f4d5cf9cd42fe9befd435f63fc5925e1e; JSESSIONID=46A289C241B5768EDD4CCFF2B14C70E9; Hm_lvt_346e80875a7d4528ffaa7b871907c117=1633682839,1633743407,1633913167; Hm_lpvt_346e80875a7d4528ffaa7b871907c117=1633914088");
//        httpPost.addHeader("host", "www.qiyekexie.com");
//        httpPost.addHeader("origin","https://www.qiyekexie.com");   ///
//        httpPost.addHeader("referer", "https://www.qiyekexie.com/portal/zkx/6/expert/expertList.action"); ///
//        httpPost.addHeader("sec-fetch-dest", "empty");
//        httpPost.addHeader("sec-fetch-mode", "cors");
//        httpPost.addHeader("sec-fetch-site", "same-site");
        /** 只需要这个 user-agent 请求头就足够了 */
        httpPost.addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.366");
//        httpPost.addHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36");

        // 模仿浏览器的 表单提交数据
        String request_payload = "{\"pindex\":1, \"pageSize\":6, \"key\":\"\", \"title\":\"\", \"status\":2, \"provinceCode\":\"\", \"cityCode\":\"\", \"industry\":\"\"}";

        StringEntity stringEntity = new StringEntity(request_payload, ContentType.APPLICATION_JSON);        // 推荐的方法


        // 设置提交表单的Entity对象到Post请求中
        httpPost.setEntity(stringEntity);

        CloseableHttpResponse response = null;

        try {
            // 使用httpClient发起请求 获取 response
            response = httpClient.execute(httpPost);

            // 解析响应
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "utf8");
                System.out.println("\n 请求结果返回hhhhh::::  \n " + content + "\n");

                ///////////////////   返回的接口字符串解析   /////////////////////////
                System.out.println();
                System.out.println();

                // 把字符串转换为json对象
                JSONObject jsonObject = JSON.parseObject(content);
                System.out.println(jsonObject.get("data"));

                // 通过已知的 key 获取 val
                String s = jsonObject.get("data").toString();

                // 取下一层的数据
                JSONObject jsonObjectRes = JSONObject.parseObject(s);
                System.out.println(jsonObjectRes.get("list"));

                String lists = jsonObjectRes.get("list").toString();

                JSONArray jsonArray = new JSONArray(lists);  // 导入的包是 import org.json.JSONArray;

                System.out.println("列表的长度：：：" + jsonArray.length());

                List<Professor> professorList = new ArrayList<>();

                System.out.println("输出单个数据：： " + jsonArray.get(0).toString());

                for (int i = 0; i < jsonArray.length(); i++) {
                    Professor professor = new Professor();

                    System.out.println("\n直接保存Id即可：：：" + jsonArray.getJSONObject(i).get("id").toString());

                    professor.setId(Long.parseLong(jsonArray.getJSONObject(i).get("id").toString()));  // 这里只要id就行了
//                    professor.setName(jsonArray.getJSONObject(i).get("name").toString());
//                    professor.setLogo("https://www.qiyekexie.com/kypic/90*90" + jsonArray.getJSONObject(i).get("logo").toString());  // 头像图片地址拼接
//                    professor.setTitle(jsonArray.getJSONObject(i).get("title").toString());
//                    professor.setCompany(jsonArray.getJSONObject(i).get("company").toString());
//                    professor.setIndustryName(jsonArray.getJSONObject(i).get("industryName").toString());
//                    professor.setIndustrySecondName(jsonArray.getJSONObject(i).get("industrySecondName").toString());
//                    professor.setIndustryThirdName(jsonArray.getJSONObject(i).get("industryThirdName").toString());
//                    professor.setProvinceName(jsonArray.getJSONObject(i).get("provinceName").toString());
//                    professor.setCityName(jsonArray.getJSONObject(i).get("cityName").toString());
//                    professor.setSkimNum(jsonArray.getJSONObject(i).get("skimNum").toString());
//                    professor.setPraiseNum(jsonArray.getJSONObject(i).get("praiseNum").toString());
//                    professor.setCollectNum(jsonArray.getJSONObject(i).get("collectNum").toString());
////                    professor.setHodeNames(jsonArray.getJSONObject(i).get("hodeNames").toString());

                    professorList.add(professor);
                }

                System.out.println();
                System.out.println();

                for (int j = 0; j < jsonArray.length(); j++) {
//                    System.out.println(professorList.get(j).toString()); // 输出对象
//                    System.out.println(professorList.get(j).getId() + "");
                }

                System.out.println();
                System.out.println();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                // 关闭httpClient
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                // 关闭response
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}


